using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;

using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ACM.cmsarea08.Controls
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:RatingControl runat=server></{0}:RatingControl>")]
    public class RatingControl : TextBox
    {

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ImageSrc
        {
            get
            {
                object o = ViewState["ImageSrc"];
                if (o == null) return "/template/img/rating/";
                else return o.ToString();
            }
            set
            {
                ViewState["ImageSrc"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int RateLength
        {
            get
            {
                object o = ViewState["RateLength"];
                if (o == null) return 10;
                else return Convert.ToInt16(o.ToString());
            }
            set
            {
                ViewState["RateLength"] = value;
            }
        }        
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Value
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value;
            }
        }        

		public RatingControl()
		{
            

			
		}
		
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string ratingurl = "/template/plugins/RatingControl/rating.js";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.js");
            string cssratingurl = "/template/plugins/RatingControl/rating.css";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.css");
           // HttpContext.Current.Response.Write("--> " + embedobjecturl);

            if(this.Page is ACM.cmsarea08.Core.MainPage) {
//            	((ACM.cmsarea08.Core.MainPage)this.Page).RegisterMooTools();
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-core.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-more.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript(ratingurl);
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomCss(cssratingurl);
            } else {
            	//includi lo script... mootools..
            	if(this.Page.Header!=null) {
            		System.Web.UI.HtmlControls.HtmlHead myhead = this.Page.Header;
					bool foundMo = false;
                    bool foundMoMore = false;
					bool foundEb = false;
                    bool foundEbc = false;
					for(int i = 0; i <myhead.Controls.Count;i++) {
						if(myhead.Controls[i] is LiteralControl) {
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-core.js") != -1)
                            {
								foundMo=true;
							}
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-more.js") != -1)
                            {
                                foundMoMore = true;
                            }

							if(((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("rating.js")!=-1) {
								foundEb=true;
							}
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("rating.css") != -1)
                            {
                                foundEbc = true;
                            }

						}
					}
					if(!foundMo) {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-core.js\"></script>");
						myhead.Controls.Add(ct);			
					}
                    if (!foundMoMore)
                    {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-more.js\"></script>");
                        myhead.Controls.Add(ct);
                    }            		

					if(!foundEb) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\""+ratingurl+"\"></script>");
						myhead.Controls.Add(ct);			
					}
                    if (!foundEbc)
                    {
                        LiteralControl ct = new LiteralControl("<style type=\"text/css\" media=\"screen\">\n"+
                            "<!--\n"+
                            "@import url(\""+cssratingurl+"\");\n"+
                            "-->\n</style>");
                        myhead.Controls.Add(ct);
                    }             		
            		
            	}
            }
        }		
        protected override void Render(HtmlTextWriter writer)
        {
			//this.ID
			//this.ClientID
			//this.UniqueID
			int maxRate = this.RateLength;
			if(maxRate>10)
				maxRate = 10;
			writer.Write("<div id=\""+this.ClientID+"contaiter\" class=\"ratingcontrol\" >");
			writer.Write("<input type=\"hidden\" value=\""+this.Value+"\" id=\"" + this.ClientID + "\" name=\""+this.UniqueID+"\">\n");
			writer.Write("<ul id=\""+this.ClientID+"ratingul\" class=\"rating nostar\" style=\"width:"+(maxRate*16)+"px;\"> ");
			for(int i = 0; i<maxRate; i++) {
				string point = "";
				switch ( (i+1) ) {
					case 1: point = "one";break;
					case 2: point = "two";break;
					case 3: point = "three";break;
					case 4: point = "four";break;
					case 5: point = "five";break;
					case 6: point = "six";break;
					case 7: point = "seven";break;
					case 8: point = "height";break;
					case 9: point = "nine";break;
					case 10: point = "ten";break;
				}
				writer.Write("<li id=\""+(i+1)+"\" class=\"rate "+point+"\"><a name=\"rate\" href=\"#rate\" title=\""+(1+i)+" Star\">"+(i+1)+"</a></li> ");
			}
			writer.Write("</ul>");
			
			writer.Write("</div>\n");

            string img_path = this.ImageSrc;
		    writer.Write("<script language=\"javascript\" type=\"text/javascript\" defer=\"defer\">\n");
		    writer.Write("window.addEvent('domready', function() {\n");
            writer.Write("var " + this.ID + "_RatingControl = new RatingControl( {baseID:'" + this.ClientID + "', value:'" + this.Value + "', " +
		                 "imagesrc:'" + img_path + "'});\n");
		    writer.Write("});");
            writer.Write("</script>\n");
		    
		    
		    
		    
		    

        }
        
	}
}
